/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.modules;

import alcatelparser.modules.io.FileWriter;
import alcatelparser.tags.ITagParser;
import alcatelparser.tags.SupplementaryServicesTag;
//import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author geniuz
 */
public class CSVBuilder
    implements IReducedBuilder
{

    public CSVBuilder()
    {
        _isHeaderOver = false;
        _recordStart = true;
        //LOG = Logger.getLogger(com/alcatel/algo/out/CSVBuilder);
        _emptyPrinter = new EmptyTagPrinter(this);
    }

    public final void buildHeader(int ai[], int i, int j)
    {
        _emptyPrinter.release();
        try
        {
            _fos.write(I.I(4062));
        }
        catch(IOException ioexception)
        {
            //LOG.error(ioexception);
        }
    }

    public final void writeTagContent(int i, int j, String s, String s1)
    {
        if(isHeaderOver())
        {
            if(_recordStart && Env.getEnv().isRecordNumbering())
                try
                {
                    _recordStart = false;
                    _fos.write((new StringBuilder()).append(_fos.getRecordNumber()).append(I.I(8716)).toString());
                }
                catch(IOException ioexception)
                {
                    //LOG.error(ioexception);
                }
            _emptyPrinter.printEmptyTag(s);
            writeEmptyTagContent(i, j, s, s1);
        }
    }

    public final void writeEmptyTagContent(int i, int j, String s, String s1)
    {
        try
        {
            String s2 = (new StringBuilder()).append(I.I(8718)).append(s1).append(I.I(8721)).append(I.I(8716)).toString();
            _fos.write(s2);
        }
        catch(IOException ioexception)
        {
            //LOG.error(ioexception);
        }
    }

    private boolean isHeaderOver()
    {
        TagFactory tagfactory = (TagFactory)Env.getEnv().get(I.I(7894));
        return _isHeaderOver || !tagfactory.isHeaderIsParsing();
    }

    public final void writeSummary(int i)
    {
        try
        {
            if(_fos != null)
                _fos.writeSummary(null);
        }
        catch(IOException ioexception)
        {
            //LOG.error(ioexception);
        }
    }

    public final void writeOverAllHeader(String outpath)
    {
        try
        {
            _fos = new FileWriter(outpath);
            CSVHeaderBuilder csvheaderbuilder = new CSVHeaderBuilder();
            csvheaderbuilder.constructOverallHeader();
            _fos.writeOverallHeader(csvheaderbuilder.getHeader());
        }
        catch(Exception ex){
        }
    }

    public final void tagParseEnd(ITagParser itagparser)
    {
        if(itagparser instanceof SupplementaryServicesTag)
        {
            SupplementaryServicesTag supplementaryservicestag = (SupplementaryServicesTag)itagparser;
            (new SupplementaryServiceEmptyTagPrinter()).printEmptyTag(supplementaryservicestag.getServiceNumber(), this);
        }
    }

    public final void releaseContent()
    {
        _isHeaderOver = false;
        try
        {
            if(_fos != null)
                _fos.close();
        }
        catch(IOException ioexception)
        {
           // LOG.error(ioexception);
        }
    }

    public final long getWroteSize()
    {
        if(_fos == null)
            return 0L;
        else
            return _fos.getWroteSize();
    }

    public final int getRecordWroteAmount()
    {
        if(_fos == null)
            return 0;
        else
            return _fos.getWroteRecordsNumber();
    }

    public final void notifyRecordEnd()
    {
        try {
            _fos.notifyRecordEnd();
            _recordStart = true;
        } catch (FileNotFoundException ex) {
            //Logger.getLogger(CSVBuilder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            //Logger.getLogger(CSVBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public final void notifyHeaderEnd(int i, int j, int ai[])
    {
        _isHeaderOver = true;
    }

    private boolean _isHeaderOver;
    private boolean _recordStart;
    private EmptyTagPrinter _emptyPrinter;
    private FileWriter _fos;
//    private Logger LOG;
}
